package client;


import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextPane;
import javax.swing.Timer;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;

import common.DB;


@SuppressWarnings("serial")
public class Message extends JFrame{
    public static Dimension  screenSize    = Toolkit.getDefaultToolkit().getScreenSize();
    public static int        screen_h      = (int) screenSize.getHeight();                ;
    public static int        screen_w      = (int) screenSize.getWidth();

    private JTextPane        textPane;
    private static int       openedMessage = 0;
    private static int       height        = 160;
    private static int       width         = 260;
    private static Integer[] posotions     = new Integer[screen_h / height];

    public static void start(final String msg) {
        EventQueue.invokeLater(new Runnable(){
            public void run() {
                try {
                    Message frame = new Message(msg);
                    frame.open();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public Message(String message) {
        this();
        this.textPane.setText(message);
    }

    public Message(JFrame parent, String message) {
        this(message);
        parent.getContentPane().add(this);
    }

    public void open() {
        toFront();
        final Message self = this;

        final float delta = 0.5f;

        ActionListener actionListener = new ActionListener(){
            private int step = 0;

            public void actionPerformed(ActionEvent actionEvent) {
                if ((openedMessage + 1) * height >= screen_h)
                    openedMessage = 1;
                int newWidth = (int) ((++step) * step * delta - step * delta * 5 + 5);
                if (newWidth <= (width + 10)) {
                    self.setLocation(screen_w - newWidth, height * (openedMessage - 1));
                } else {
                    ((Timer) actionEvent.getSource()).stop();
                }
            }
        };
        Timer timer = new Timer(10, actionListener);
        timer.start();
        setVisible(true);
    }

    public void close() {
        setVisible(false);
    }

    public Message() {
        if (posotions.length >= openedMessage) {
            openedMessage++;
        } else {
            openedMessage = 1;
        }
        setIconImage(Toolkit.getDefaultToolkit().getImage(Message.class.getResource("/images/icon_27x27.png")));
        setUndecorated(true);
        setBackground(new Color(255, 255, 255, 0));

        setResizable(false);
        setAlwaysOnTop(true);
        final Message self = this;
        setTitle(DB.getText("msg_from_infomir"));
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(screen_w, screen_h - height * openedMessage - 40, width, height);
        getContentPane().setLayout(new GridLayout(1, 0, 0, 0));

        JPanel panel = new JPanel();
        panel.setEnabled(false);
        panel.setBorder(new CompoundBorder(new LineBorder(new Color(160, 160, 160), 2, true), new LineBorder(new Color(240, 240, 240), 2, true)));
        getContentPane().add(panel);
        panel.setLayout(null);

        JButton btnX = new JButton("");
        btnX.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent arg0) {
                openedMessage--;
                self.dispose();
            }
        });
        btnX.setIcon(new ImageIcon(Message.class.getResource("/javax/swing/plaf/metal/icons/ocean/paletteClose-pressed.gif")));
        btnX.setMaximumSize(new Dimension(30, 20));
        btnX.setOpaque(false);
        btnX.setPreferredSize(new Dimension(30, 15));
        btnX.setBounds(225, 11, 26, 26);
        panel.add(btnX);

        JPanel panel_2 = new JPanel();
        panel_2.setBounds(5, 11, 250, 105);
        panel.add(panel_2);
        panel_2.setLayout(null);

        JLabel label = new JLabel(DB.getText("msg_from_infomir"));
        label.setBorder(new EmptyBorder(10, 10, 10, 10));
        label.setBounds(new Rectangle(5, 5, 5, 5));
        label.setBounds(0, 0, 216, 33);
        panel_2.add(label);
        label.setIcon(new ImageIcon(Message.class.getResource("/images/icon_min.png")));
        label.setFont(new Font("Times New Roman", Font.BOLD, 14));

        JPanel panel_3 = new JPanel();
        panel_3.setBorder(new TitledBorder(null, "", TitledBorder.LEADING, TitledBorder.TOP, null, null));
        panel_3.setBounds(10, 31, 230, 74);
        panel_2.add(panel_3);
        panel_3.setLayout(null);

        textPane = new JTextPane();
        textPane.setBounds(5, 5, 218, 65);
        panel_3.add(textPane);
        textPane.setMargin(new Insets(0, 0, 0, 0));
        textPane.setBackground(new Color(255, 255, 255, 0));
        textPane.setText("");
        textPane.setOpaque(false);
        textPane.setFont(new Font("Times New Roman", Font.PLAIN, 14));
        textPane.setEditable(false);

        JButton btnNewButton = new JButton(DB.getText("i_read"));
        btnNewButton.setFont(new Font("Times New Roman", Font.BOLD, 14));
        btnNewButton.setIcon(new ImageIcon(Message.class.getResource("/images/Nimbuzz-Notifiable.png")));
        btnNewButton.setBounds(48, 122, 150, 26);
        panel.add(btnNewButton);
        btnNewButton.setMargin(new Insets(2, 0, 2, 0));
        btnNewButton.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent arg0) {
                openedMessage--;
                self.dispose();
            }
        });
    }
}
